package cn.ecut.dao;

import cn.ecut.exception.DaoException;
import cn.ecut.model.Good;
import cn.ecut.model.User;
import cn.ecut.utils.JDBCHelper;
import cn.ecut.utils.TableNames;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodDao {
    private String column = " (goodname,goodtime,goodexplain,goodpicture,fabuid,fabucontact,fabutime) " ;
    private UserDao userDao = new UserDao() ;
    public Good loadByGoodId(Integer goodId ){
        String SQL = "select * from " + TableNames.GOOD_TABLE_NAME + " where goodid = ? " ;
        ResultSet resultSet = JDBCHelper.query(SQL, goodId);
        List<Good> goodList=warp(resultSet);
        if(goodList.size()>0)
            return goodList.get(0);
        else
            return null;
    }
    public List<Good> loadByUserId(Integer userId ){
        String SQL = "select * from " + TableNames.GOOD_TABLE_NAME + " where fabuid = ? " ;
        ResultSet resultSet = JDBCHelper.query(SQL, userId);
        List<Good> goodList = warp(resultSet);
        return goodList;
    }
//    public List<Good> listTop( Integer num ) {
//        String SQL = "select * from" + TableNames.GOOD_TABLE_NAME + " order by fabutime desc limit ? , ? " ;
//        ResultSet resultSet = JDBCHelper.query(SQL, 0, num);
//        List<Good> warp = warp(resultSet);
//        return warp ;
//    }
    public boolean saveGood(Good good ){
        String SQL = "insert into " + TableNames.GOOD_TABLE_NAME +column+ " values (?,? ,? ,?, ?, ?, ? )" ;
        return JDBCHelper.execute( SQL ,good.getGoodname(),good.getGoodtime(),
                good.getGoodexplain(),good.getGoodpicture(),good.getUser().getUserid(),good.getUser().getUserphone(),good.getFabutime()) ;
    }
    public boolean updateGoodState(Integer goodId)
    {
        //UPDATE `good` SET `goodstate` = '1' WHERE `good`.`goodid` = 13;
        String SQL="update "+TableNames.GOOD_TABLE_NAME+" set goodstate = '1' where goodid= " +goodId;
        return JDBCHelper.execute(SQL);
    }
//    public boolean updateGoodState2(String state,Integer goodId)
//    {
//        //UPDATE `good` SET `goodstate` = '1' WHERE `good`.`goodid` = 13;
//        String SQL="update "+TableNames.GOOD_TABLE_NAME+" set goodstate = "+state+" where goodid= " +goodId;
//        return JDBCHelper.execute(SQL);
//    }
    public boolean deleteGood(Integer goodId) {
        String SQL="delete from "+TableNames.GOOD_TABLE_NAME+" where goodid="+goodId;
        return JDBCHelper.execute(SQL);
    }
    private List<Good> warp(ResultSet resultSet) {
        List<Good> list = new ArrayList<>() ;
        try {
            while( resultSet.next() ){
                Good g = new Good() ;
                g.setGoodid( resultSet.getInt("goodid") ); ;
                g.setGoodname( resultSet.getString("goodname") );
                g.setGoodtime(resultSet.getTimestamp("goodtime"));
                g.setGoodexplain(resultSet.getString("goodexplain"));
                g.setGoodpicture(resultSet.getString("goodpicture"));
                g.setGoodstate(resultSet.getString("goodstate"));
                User user = userDao.loadByUserId(resultSet.getInt("fabuid"));
                g.setUser( user );
                g.setFabutime(resultSet.getTimestamp("fabutime"));
                list.add( g ) ;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new DaoException("数据封装出错。") ;
        }
        return list ;
    }
}
